#include "gwbase/base/atomic.h"
#include "gwbase/base/thread.h"
#include "gwbase/base/thread_pool.h"
#include "gwbase/base/test_harness.h"
#include <boost/bind.hpp>

using namespace dicom;

class Worker {
 public:
  void DoWork() {
    count_.Increment();
  }

  int count() { return count_.Load_Acquire(); }

 private:
  AtomicInt32 count_; 
};

class ThreadPoolTest { };

TEST(ThreadPoolTest, SingleThread) {
  Worker worker;
  ThreadPool pool("single", 1);
  pool.Start();
  for (int i = 0; i < 1000; i++) 
    pool.RunTask(boost::bind(&Worker::DoWork, &worker));

  sleep(1);
  pool.Stop();

  EXPECT_Eq(worker.count(), 1000);
}

TEST(ThreadPoolTest, MultiThread) {
  Worker worker;
  ThreadPool pool("multiple", 4);
  pool.Start();
  for (int i = 0; i < 10000; i++) 
    pool.RunTask(boost::bind(&Worker::DoWork, &worker));

  sleep(1);
  pool.Stop();

  EXPECT_Eq(worker.count(), 10000);
}

int main() {
  dicom::RunAllTests();
  return 0;
}
